package com.gxa.saas.controller;

import com.gxa.saas.pojo.Measure;
import com.gxa.saas.service.MeasureService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @Author: yx
 * @Date: 2022/6/7 11:45
 */
@RestController
@RequestMapping("/measure")
@CrossOrigin
public class MeasureController {

    @Autowired
    MeasureService measureService;

    /**
     * 查询全部
     * @return
     */
    @ApiOperation("查询全部")
    @PostMapping("/selectAll")
    public Map selectAll(){
        List<Measure> measures = measureService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(measures);
        return jsonResult.getMap();
    }

    /**
     * 单位的分页查询方法
     *
     * @param offset
     * @return
     */
    @ApiOperation("单位的分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "offset", value = "查询页数", paramType = "query"),
    })
    @PostMapping("/selectByPage")
    public Map selectByPage(@RequestParam("offset") Integer offset) {
        List<Measure> measures = measureService.selectByPage(offset);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(measures);
        return jsonResult.getMap();
    }

    /**
     * 添加单位的方法
     *
     * @param measure
     * @return
     */
    @ApiOperation(value = "添加单位")
    @ApiImplicitParam(name = "measure", value = "要添加的单位对象", paramType = "body", dataType = "Measure")
    @PostMapping("/insert")
    public Map insert(@RequestBody Measure measure) {
        measureService.insert(measure);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }

    /**
     * 根据单位名称name修改的方法
     *
     * @param measure
     */
    @ApiOperation(value = "修改单位信息", produces = "update")
    @ApiImplicitParam(name = "measure", value = "修改好的单位信息", paramType = "body", dataType = "Measure")
    @PostMapping("/update")
    public Map update(@RequestBody Measure measure) {
        measureService.update(measure);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    /**
     * 根据单位名称删除的方法
     *
     * @param id
     */
    @ApiOperation(value = "根据单位id删除", produces = "delete")
    @ApiImplicitParam(name = "id", value = "单位编号")
    @PostMapping("/delete")
    public Map delete(@RequestParam("id") Integer id) {
        measureService.delete(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "是否启用", produces = "updateIsUsed")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单位编号"),
            @ApiImplicitParam(name = "isUsed", value = "是否启用")
    })
    @PostMapping("/updateIsUsed")
    public Map updateIsUsed(@RequestParam("id") Integer id, @RequestParam("isUsed") Boolean isUsed) {
        measureService.updateIsUsed(id, isUsed);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }



}
